<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <H1>question1</H1>
<script>
    var persion=[
        {
            id:1,
             name:'jerry',
             age:20,

        },
        {
            id:3,
            name:'hery',
            age:18
        },
        {
            id:4,
            name:'marry',
            age:31
        },
        {
            id:2,
            name:'yang',
            age:16
        }
    ];
    function sort(arr,ss,dir){
        for(var j=0;j<arr.length;j++){
            if(ss=='id'){
                arr[j].xx=arr[j].id;
            }else if(ss=='age'){
                arr[j].xx=arr[j].age;
            }
        }
        var p,m;
        for(var i=1;i<arr.length;i++){

            p=i-1;
            m=arr[i];
            while(p>=0&&arr[p].xx>m.xx){
                arr[p+1]=arr[p];
                p--;
            }
             if(ss=='id'){
                arr[i].xx=arr[i].id;
            }else{
                arr[i].xx=arr[i].age;
            }arr[p+1]=m;
        }
        if(dir=='dir'){
            return arr;
        }else if(dir=='sdir'){
            arr.reverse();
            return arr;
        }
    }

    console.log( sort(persion,'id','sdir'));
</script>
</body>
</html>